package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.information.StopInformationBo;
import com.zhuss.bean.vo.core.information.DetailInformationVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.InformationDao;
import com.zhuss.core.db.entity.Information;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 创建通知公告 processor
 * @Author: zhushanshuo
 * @Date: Tue Feb 02 15:16:28 CST 2021
 */
@Component("informationStop")
public class InformationStop extends AbstractProcessor<Object, StopInformationBo> implements Processor<Object, StopInformationBo> {

    @Autowired
    private InformationDao informationDao;

    @Autowired
	@Qualifier("informationRead")
	private InformationRead informationRead;

    @Autowired
    private InformationCheck informationCheck;

    /**
     * 校验
     */
    @Override
    public void check(StopInformationBo bo) throws DzcxException {
		if (bo.getInformationId() == null ||bo.getInformationId() <0l) {
			throw new DzcxException(ExceptionCode.通知公告ID为空);
		}
    	// 当前通知公告不满足停用条件
		if (!informationCheck.checkStop(bo.getInformationId())) {
			throw new DzcxException(ExceptionCode.当前通知公告不满足停用条件);
		}
    }

    /**
     * 操作方法
     * @throws DzcxException
     */
    @Override
    public Object process(StopInformationBo param) throws DzcxException {

    	Long informationId = param.getInformationId();

    	Information record = new Information();
    	record.setInformationId(param.getInformationId());
    	record.setStatus(param.getStatus());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	informationDao.updateByPrimaryKeySelective(record);

    	String name = ((DetailInformationVo) informationRead.detail(informationId)).getTitle();

    	param.setName(name);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(StopInformationBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.通知公告停用.key());
    	record.setTargetId(param.getInformationId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
